<?php

namespace App\Repositories;

use App\Models\AttributeType;
use App\Models\Cart;
use App\Models\Goods;
use App\Models\GoodsAttribute;
use App\Models\GoodsAttributeType;
use App\Models\Order;
use App\Models\OrderGoods;
use Illuminate\Support\Facades\Auth;
use InfyOm\Generator\Common\BaseRepository;

/**
 * Class GoodsRepository
 * @package App\Repositories
 * @version September 14, 2017, 7:41 pm UTC
 *
 * @method Goods findWithoutFail($id, $columns = ['*'])
 * @method Goods find($id, $columns = ['*'])
 * @method Goods first($columns = ['*'])
*/
class GoodsRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'sn',
        'name',
        'click_count',
        'sales_count',
        'brand',
        'provider_name',
        'number',
        'market_price',
        'sale_price',
        'unit',
        'sale_start_date',
        'sale_end_date',
        'is_on_sale',
        'is_best',
        'is_new',
        'is_hot',
        'sort',
        'desc',
        'image'
    ];

    /**
     * Configure the Model
     **/
    public function model()
    {
        return Goods::class;
    }

    public function checkNumber($number,$goods,$attribute)
    {
        if($attribute && ($attribute instanceof GoodsAttribute)){
            if($attribute->number>=$number){
                return true;
            }
        }else{
            if($goods->number>=$number){
                return true;
            }
        }
        return false;
    }

    public function checkDayNumber($number,$goods,$attribute)
    {
        $user_id = Auth::id();
        if($attribute && ($attribute instanceof GoodsAttribute)){
            $number += Cart::where('attribute_id', $attribute->id)->where('user_id', $user_id)->sum('number');
            $order_ids = Order::where([
                ['status','>=',2],
                ['user_id',$user_id],
                ['status','<',10],
                ['created_at','>=',date('Y-m-d')],
                ['created_at','<=',date('Y-m-d',time()+3600*24)],
            ])->pluck('id')->toArray();
            $number += OrderGoods::where('attribute_id', $attribute->id)->whereIn('order_id', $order_ids)->where('status','<',10)->sum('number');
            $number = (int)$number>0?(int)$number:0;
            if($attribute->day_buy_number==0)
                return true;
            if($attribute->day_buy_number>=$number){
                return true;
            }
        }else{
            $number += Cart::where('goods_id', $goods->id)->where('user_id', $user_id)->sum('number');
            $order_ids = Order::where([
                ['status','>=',2],
                ['user_id',$user_id],
                ['status','<',10],
                ['created_at','>=',date('Y-m-d')],
                ['created_at','<=',date('Y-m-d',time()+3600*24)],
            ])->pluck('id')->toArray();
            $number += OrderGoods::where('goods_id', $goods->id)->whereIn('order_id', $order_ids)->where('status','<',10)->sum('number');
            $number = (int)$number>0?(int)$number:0;
            if($goods->day_buy_number==0)
                return true;
            if($goods->day_buy_number>=$number){
                return true;
            }
        }
        return false;
    }

    public function generateAttributeTypesByGoodsId($goods_id,$shop_id=null)
    {
        if(!$shop_id){
            $shop_id = Goods::find($goods_id)->shop->id;
        }
        $types = GoodsAttributeType::where([
            ['goods_id',$goods_id],
        ])->get();
        $names = [];
        foreach ($types as $type) {
            $count = GoodsAttributeType::where([
                ['name',$type->name],
            ])->count();
            if($count>2){
                if(isset($names[$type->name])){
                    $names[$type->name] .= ','.$type->options;
                }else{
                    $names[$type->name] = $type->options;
                }
            }
        }
        foreach ($names as $name=>$options) {
            $_options = implode(',',array_unique(explode(',',$options)));
            $data = [
                'shop_id'=>$shop_id,
                'name'=>$name,
                'options'=>$_options,
            ];
            $has = AttributeType::where([
                ['shop_id',$shop_id],
                ['name',$name],
            ])->first();
            if($has){
                AttributeType::where('id',$has->id)->update($data);
            }else{
                AttributeType::create($data);
            }
        }
    }
}
